/**
 * Created by OneDream on 2016/1/11.
 */
var currentAccount=localStorage.getItem("account");
var toAccount=GetQueryString("account");
var nickname=GetQueryString("nickname");
var toicon=GetQueryString("toicon");

function GetQueryString(name)
{
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if(r!=null) {
        return r[2];
    }
}
function getNowFormatDate() {
    var date = new Date();
    var seperator1 = "-";
    var seperator2 = ":";
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
        + " " + date.getHours() + seperator2 + date.getMinutes()
        + seperator2 + date.getSeconds();
    return currentdate;
}
$(document).ready(function(){
    var currentAccount=localStorage.getItem("account");
    var w=new Worker("js/getMessage.js");
    var pram="?account="+currentAccount+"&toAccount="+toAccount;
    w.postMessage(pram);
    w.addEventListener("message",function(e){
        requestContact(e.data);
        $(".talk_record").scrollTop($("#jp-container").height()-$(".talk_record").height());
    });

    //alert(+GetQueryString("icon"));
    $(".talk_title span").html("正在与"+nickname+"聊天");
    $(".talk_recordboxme  img").attr("src","image/"+GetQueryString("icon")+".jpg");
    //if(currentAccount!=null){
    //    $.ajax({
    //        url: "http://192.168.1.11:8080/WeiXin/getMessageLog",
    //        data: {
    //            "account": currentAccount,
    //            "toAccount": toAccount
    //        },
    //        async: true,
    //        dataType: "json",
    //        success: function (data) {
    //            if (data.status == 0) {
    //                requestContact(data.data);
    //              //  alert(data.data.length)
    //                //alert("聊天记录");
    //            } else {
    //                alert("请求聊天记录失败");
    //            }
    //
    //        },
    //        error: function () {
    //            alert("网络异常");
    //        }
    //
    //    });}
    $("#back").click(function(){
        //localStorage.removeItem("account");
        localStorage.setItem("iframesrc","message_recorded")
        window.location.href="index1.html";
    });
    $("#message").focus(function(){
        $(".talk_record").scrollTop($("#jp-container").height()-$(".talk_record").height());
    });
    $(".talk_send").click(function(){
        $(".talk_record").scrollTop($("#jp-container").height()-$(".talk_record").height());
        var content=$(".messages").val()
        var time=new Date();
        $(".messages").val("");
        if(content=="在这里输入文字"){
            alert("发送内容不能为空");
        }else{
            $.ajax({
                url: "http://192.168.1.11:8080/WeiXin/saveMessage",
                type:"post",
                data: $.toJSON({
                    "account": currentAccount,
                    "toaccount":toAccount,
                    "mynickname":localStorage.getItem("nickname"),
                    "tonickname":nickname,
                    "myicon":localStorage.getItem("icon"),
                    "toicon":toicon,
                    "message":content,
                    "time":getNowFormatDate()
                }),
                "contentType": "application/json",
                async: true,
                dataType: "json",
                success: function (data) {
                    //alert(data);
                },
                error: function () {
                    alert("网络异常");
                }

            });
        }
    });
    //$("#nickname").html("昵称："+GetQueryString("nickname"));
    //$("#address").html("地区："+GetQueryString("address"));
});
function requestContact(data){
    $.each(data,function(i,item){
        if(item.account==currentAccount){
            var talk_recordboxme=$("<div class='talk_recordboxme'></div>");
            $("#jp-container").append(talk_recordboxme);
            var user=$("<div class='user'></div>");
            talk_recordboxme.append(user);
            var img=$("<img src=''/>");
            var name=$("<span></span>")
            user.append(img);
            user.append(name);
            //var talk_recordtextbg=$("<div class='talk_recordtextbg'></div>");
            //talk_recordboxme.append(talk_recordtextbg);
            var talk_recordtext=$("<div class='talk_recordtext'></div>");
            talk_recordboxme.append(talk_recordtext);
            var text=$("<h3></h3>");
            var talk_time=$("<span class='talk_time'></span>");
            talk_recordtext.append(text);
            talk_recordtext.append(talk_time);
            img.attr("src","image/"+item.myicon+".jpg");
            text.html(item.message);
            name.html(item.mynickname);
            talk_time.html(item.time);

        }else{

            var talk_recordbox=$("<div class='talk_recordbox'></div>")
            $("#jp-container").append(talk_recordbox);
            var user=$("<div class='user'></div>");
            talk_recordbox.append(user);
            var img=$("<img src=''/>");
            var name=$("<span></span>")
            user.append(img);
            user.append(name);
            //var talk_recordtextbg=$("<div class='talk_recordtextbg'></div>");
            //talk_recordbox.append(talk_recordtextbg);
            var talk_recordtext=$("<div class='talk_recordtext'></div>");
            talk_recordbox.append(talk_recordtext);
            var text=$("<h3></h3>");
            var talk_time=$("<span class='talk_time'></span>");
            talk_recordtext.append(text);
            talk_recordtext.append(talk_time);
            img.attr("src","image/"+item.myicon+".jpg");
            name.html(item.mynickname);
            text.html(item.message);
            talk_time.html(item.time);
        }
    });

}

